package MyGO.controller;

import MyGO.Data.GlobalData;
import MyGO.Form.TrackForm;
import MyGO.Main;
import MyGO.controller.MyController.PushWindow;
import MyGO.service.ITrackFormService;
import MyGO.service.impl.TrackFormServiceImpl;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Modality;
import javafx.stage.Stage;
import lombok.Setter;

import java.io.IOException;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicReference;

//import static MyGO.utils.ArrayfilterUtils.ArrayfilterUtils.FilterByTrackFormID;
import static MyGO.controller.MyController.PushWindow.pushOnlyPrompt;
import static MyGO.utils.ArrayfilterUtils.ArrayfilterUtils.*;

public class TableViewController {
    @Setter
    private String filtStatus = null;
    @Setter
    private String filtDoctorID = null;
    @Setter String status = null;

    public TableViewController(String filtStatus, String filtDoctorID, String status) {  // 两层过滤
        this.filtStatus = filtStatus;
        this.filtDoctorID = filtDoctorID;
        this.status = status;
    }

    public TableViewController(String filtStatus) {     // 一层过滤
        this.filtStatus = filtStatus;
    }
    @FXML
    private TextField ID_Search;
    @FXML
    private TextField name_Search;
    @FXML
    private TableView<TrackForm> tv_TrackForm;
    @FXML
    private TableColumn<TrackForm, String> IDCloumn;
    @FXML
    private TableColumn<TrackForm, Number> codeCloumn;
    @FXML
    private TableColumn<TrackForm, String> nameCloumn;
    @FXML
    private Button search;
    @FXML
    private Button flash;

    private StringProperty tf_searchID = new SimpleStringProperty();
    private StringProperty tf_searchName = new SimpleStringProperty();

    private ITrackFormService trackFormService = new TrackFormServiceImpl();
    private List<TrackForm> trackForms;

    @FXML
    public void initialize() {
        if (!Objects.equals(status, "3")) {

        }
        if (filtDoctorID == null && filtStatus != null) {
            trackForms = trackFormService.selectTrackFormByStatus(filtStatus);
        } else {
            trackForms = trackFormService.selectTrackFormByStatusAndDoctorID(filtStatus, filtDoctorID);
//            trackForms = trackFormService.selectTrackFormByStatusAndDoctorID("未叫号", "111");
        }
        IDCloumn.setCellValueFactory(new PropertyValueFactory<>("id"));
        codeCloumn.setCellValueFactory(cellData ->
                new SimpleIntegerProperty(tv_TrackForm.getItems().indexOf(cellData.getValue()) + 1)
        );
        nameCloumn.setCellValueFactory(new PropertyValueFactory<>("name"));
        tv_TrackForm.getItems().addAll(trackForms);

        ID_Search.textProperty().bindBidirectional(tf_searchID);
        name_Search.textProperty().bindBidirectional(tf_searchName);


        tf_searchID.addListener((observable, oldValue, newValue) -> {
//            List<TrackForm> lts = FilterByTrackFormID(trackForms, newValue);
            String nameValue = tf_searchName.getValue();
            List<TrackForm> lts = FilterTrackFormByIDAndPatientName(trackForms, newValue, nameValue);
            if (lts != null) {
                // 删除原有数据
                tv_TrackForm.getItems().clear();
                // 填充数据
                tv_TrackForm.getItems().addAll(lts);
            }
        });

        tf_searchName.addListener((observable, oldValue, newValue) -> {
            String IDValue = tf_searchID.getValue();
//            List<TrackForm> lns = trackFormService.selectTrackFormByName(newValue);
            List<TrackForm> lns = FilterTrackFormByIDAndPatientName(trackForms, IDValue, newValue);
            System.out.println(newValue);
            if (lns != null) {
                // 删除原有数据
                tv_TrackForm.getItems().clear();
                // 填充数据
                tv_TrackForm.getItems().addAll(lns);
            }
        });
    }

    public void tf_searchNameReleased(KeyEvent event) {
        if (event.getCode().equals(KeyCode.DOWN)) {
            // 表格获取焦点
            tv_TrackForm.requestFocus(); // 设置焦点，生成视觉效果，获取键盘输入
            tv_TrackForm.getSelectionModel().selectFirst();  // 调用TableView的选择模型，并选中第一行
        }
        if (event.getCode().equals(KeyCode.UP)) {
            if (tv_TrackForm.getSelectionModel().getSelectedIndex() == 0) {
                name_Search.requestFocus();
                name_Search.selectAll();
            }
        }
    }

    public void tf_searchIDReleased(KeyEvent event) {
        if (event.getCode().equals(KeyCode.DOWN)) {
            // 表格获取焦点
            tv_TrackForm.requestFocus(); // 设置焦点，生成视觉效果，获取键盘输入
            tv_TrackForm.getSelectionModel().selectFirst();  // 调用TableView的选择模型，并选中第一行
        }
        if (event.getCode().equals(KeyCode.UP)) {
            if (tv_TrackForm.getSelectionModel().getSelectedIndex() == 0) {
                ID_Search.requestFocus();
                ID_Search.selectAll();
            }
        }
    }
    @FXML
    public void tv_searchTrackFormReleased(KeyEvent event) throws IOException {
        if (event.getCode().equals(KeyCode.ENTER)) {
            sure();
        }
    }
    @FXML
    void tv_ClickedTrackFormReleased(MouseEvent event) {
        tv_TrackForm.requestFocus();
    }

    @FXML
    void choose(ActionEvent event) throws IOException {
        sure();
    }

    @FXML
    void Flash(ActionEvent event) {
        trackForms = trackFormService.selectTrackFormByStatusAndDoctorID(filtStatus, filtDoctorID);
        tv_TrackForm.getItems().clear();
        tv_TrackForm.getItems().addAll(trackForms);
    }
    void sure() throws IOException {
        TrackForm trackForm = tv_TrackForm.getSelectionModel().getSelectedItem();
        if (Objects.equals(this.status, "1")) {
            trackForm.setStatus_main("叫号中");
            GlobalData.trackForm = trackForm;
        } else if (Objects.equals(this.status, "2")) {
            GlobalData.trackForm = trackForm;
            PushWindow.pushWindow("/MyGO/fxml/check_result_view.fxml", "检查结果", 800, 600,
                    CheckResultViewController.class, ((controller, stage) -> {
                        controller.setMyStage(stage);
                        controller.setTableViewController(this);
                    }));

        }
        if (Objects.equals(status, "1")) {
            pushOnlyPrompt("/MyGO/fxml/prompt_view_check_medicine.fxml", "已叫患者" + trackForm.getName(), 200.0, 100.0);
            trackForms.remove(trackForm);
            tv_TrackForm.getItems().clear();
            tv_TrackForm.getItems().addAll(trackForms);
        }
        ID_Search.clear();
        name_Search.clear();
        System.out.println(trackForm);
    }
}
